﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using BoolanSlide.RedoUndo;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml.Media;

namespace BoolanElement
{
    public class BooPentagonElement:BooTextShapesBaseElement
    {
        public PointCollection Points { get; set; }

        public override async Task<BooUIBase> Render(SaveOriginalElement saveOriginal, SaveForRedoUndo saveForRedoUndo)
        {
            BooPentagon square = new BooPentagon(this, saveForRedoUndo);
            RedoUndoHelper.SaveOriginalStatus(square);
            return square;
        }

        public override async Task<BooUIBase> RenderForThumbNails()
        {
            BooPentagon square = new BooPentagon(this);
            return square;
        }

        public static BooPentagonElement InitElementStyle()
        {

            BooPentagonElement pentagonElement = new BooPentagonElement();
            pentagonElement.ElementTransform.Rotate.Angle = 0;
            pentagonElement.ElementTransform.Rotate.CenterX = 0;
            pentagonElement.ElementTransform.Rotate.CenterY = 0;

            pentagonElement.ElementTransform.Scale.ScaleX = 1;
            pentagonElement.ElementTransform.Scale.ScaleY = 1;
            pentagonElement.ElementTransform.Scale.CenterX = 0;
            pentagonElement.ElementTransform.Scale.CenterY = 0;

            pentagonElement.ElementTransform.Translate.X = 0;
            pentagonElement.ElementTransform.Translate.Y = 0;

            pentagonElement.ElementTransform.Margin = new Windows.UI.Xaml.Thickness(0, 0, 0, 0);

            pentagonElement.ElementTransform.Width = 200;
            pentagonElement.ElementTransform.Height = 200;

            pentagonElement.TextMarginLeft = 0;
            pentagonElement.TextMarginTop = 0;
            pentagonElement.TextMaxHeight = 200;
            pentagonElement.TextWidth = 200;
           
            pentagonElement.TextAlignment = SerializeCast.StringToTextAlignment("CenterCenter");

            pentagonElement.ShapeStyle = SerializeCast.StringToTextBoxStyle("NoBorderBKG");
            pentagonElement.BorderColor = Color.FromArgb(255, 0, 0, 0);
            pentagonElement.BorderWidth = 0;

            pentagonElement.ShapeFillColor = Color.FromArgb(255, 24, 122, 169);
            pentagonElement.ShapeWidth = 200;
            pentagonElement.ShapeHeight = 200;

            pentagonElement.ControlPanelTop = 0;
            pentagonElement.ControlPanelLeft = 0;

            pentagonElement.Points = new Windows.UI.Xaml.Media.PointCollection();
            pentagonElement.Points.Add(new Point(100, 0));
            pentagonElement.Points.Add(new Point(200, 80));
            pentagonElement.Points.Add(new Point(160, 200));
            pentagonElement.Points.Add(new Point(40, 200));
            pentagonElement.Points.Add(new Point(0, 80));

            pentagonElement.Text = "";

            return pentagonElement;

        }

        public override XElement SaveToXElement()
        {
            XElement element = new XElement(typeof(BooPentagonElement).Name);
            element.SetAttributeValue("UniqueID", UniqueID.ToString());
            element.Add(ElementTransform.Save());
            XElement styleElement = SaveShapeStyle();
            //save points
            styleElement.Add(BooShapesPoints.SavePoints(Points));
            element.Add(styleElement);

            element.Add(SaveData());

            return element;
        }

        public override void LoadFromXElement(XElement element)
        {
            XElement tranformElement = element.Element("Transform");
            XElement dataElement = element.Element("Data");
            XElement styleElement = element.Element("Style");

            ElementTransform.Load(tranformElement);
            LoadShapeStyle(styleElement);
            Points = BooShapesPoints.LoadPoints(styleElement.Element("Points").Elements());
            LoadData(dataElement);
        }

        public override BooElement Clone()
        {
            BooPentagonElement element = new BooPentagonElement();

            element.ElementTransform = this.ElementTransform.Clone();

            element.TextMarginLeft = this.TextMarginLeft;
            element.TextMarginTop = this.TextMarginTop;
            element.TextMaxHeight = this.TextMaxHeight;
            element.TextWidth = this.TextWidth;
       
            element.TextAlignment = this.TextAlignment;

            element.ShapeStyle = this.ShapeStyle;
            element.BorderColor = Color.FromArgb(this.BorderColor.A, this.BorderColor.R, this.BorderColor.G, this.BorderColor.B);
            element.BorderWidth = this.BorderWidth;

            element.ShapeFillColor = Color.FromArgb(this.ShapeFillColor.A, this.ShapeFillColor.R, this.ShapeFillColor.G, this.ShapeFillColor.B);
            element.ShapeWidth = this.ShapeWidth;
            element.ShapeHeight = this.ShapeHeight;

            element.ControlPanelTop = this.ControlPanelTop;
            element.ControlPanelLeft = this.ControlPanelLeft;
            element.Text = this.Text;

            element.Points = BooShapesPoints.CopyPointCollection(this.Points);

            return element;
        }

        public override void Load(BooTextShapesBaseElement shapesElement)
        {
            BooPentagonElement ele = shapesElement as BooPentagonElement;

            this.ElementTransform = ele.ElementTransform;

            this.TextMarginLeft = ele.TextMarginLeft;
            this.TextMarginTop = ele.TextMarginTop;
            this.TextMaxHeight = ele.TextMaxHeight;
            this.TextWidth = ele.TextWidth;

            this.TextAlignment = ele.TextAlignment;

            this.ShapeStyle = ele.ShapeStyle;
            this.BorderColor = Color.FromArgb(ele.BorderColor.A, ele.BorderColor.R, ele.BorderColor.G, ele.BorderColor.B);
            this.BorderWidth = ele.BorderWidth;

            this.ShapeFillColor = Color.FromArgb(ele.ShapeFillColor.A, ele.ShapeFillColor.R, ele.ShapeFillColor.G, ele.ShapeFillColor.B);
            this.ShapeWidth = ele.ShapeWidth;
            this.ShapeHeight = ele.ShapeHeight;

            this.ControlPanelTop = ele.ControlPanelTop;
            this.ControlPanelLeft = ele.ControlPanelLeft;
            this.Text = ele.Text;

            this.Points = BooShapesPoints.CopyPointCollection(ele.Points);
        }
    }
}
